package com.cater.project.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.project.entity.DemandCommunicate;
import com.cater.project.entity.DemandEnterprise;
import com.cater.project.service.DemandCommunicateServiceImpl;
import com.cater.project.service.DemandEnterpriseServiceImpl;
import com.cater.query.CommonQuery;
import com.cater.sys.entity.Company;
import com.cater.sys.service.impl.CompanyServiceImpl;
import com.cater.sys.service.impl.ContactServiceImpl;
import com.cater.talents.entity.Contact;
import com.cater.talents.entity.ResumeProject;
import com.cater.talents.entity.TalentsRecommend;
import com.cater.talents.service.ResumeProjectServiceImpl;
import com.cater.talents.service.TalentsRecommendServiceImpl;

/**
 * 描述：企业招聘需求控制层
 * 
 * @author yong
 * @date 2018/04/25
 */
@Controller
@RequestMapping("/demandEnterprise")
public class DemandEnterpriseController {

	@Autowired
	private DemandEnterpriseServiceImpl service;
	@Autowired
	private ResumeProjectServiceImpl resumeProjectService;// 简历项目关联
	@Autowired
	private CompanyServiceImpl companyService;
	@Autowired
	private TalentsRecommendServiceImpl recommendService;
	@Autowired
	private DemandCommunicateServiceImpl communicateService;
	@Autowired
	private ContactServiceImpl contactService;
	/**
	 * @企业招聘需求管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "project/demandEnterprise/list";
	}

	/**
	 * @企业招聘需求管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询企业招聘需求分页数据")
	public PageResult<DemandEnterprise> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<DemandEnterprise> result = service.findPageResult(query);
		for (DemandEnterprise dEnterprise : result.getRows()) {
			dEnterprise.setIntervalAge(dEnterprise.getStartAge() + "-" + dEnterprise.getEndAge());
			dEnterprise.setSalary(dEnterprise.getStartSalary() + "-" + dEnterprise.getEndSalary());
			List<ResumeProject> rProjects = resumeProjectService.getByProjectId(dEnterprise.getId());
			dEnterprise.setPersons(rProjects.size());
		}
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("demandEnterprise:add")
	public String addView(String companyId, Integer tops, Model model) {
		model.addAttribute("companyId", companyId);
		model.addAttribute("tops", tops);
		return "project/demandEnterprise/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("demandEnterprise:add")
	@ResponseBody
	@SystemControllerLog(description = "添加企业招聘需求")
	public JSONObject add(DemandEnterprise t, CommonQuery query) {
		JSONObject json = new JSONObject();
		String id = service.saves(t);
		if (StringUtils.isNotBlank(id)) {
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("demandEnterprise:edit")
	public String editView(String id, Integer tops, Model model, CommonQuery query) {
		DemandEnterprise t = service.getById(id);
		model.addAttribute("demandEnterprise", t);
		model.addAttribute("tops", tops);
		return "project/demandEnterprise/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("demandEnterprise:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改企业招聘需求")
	public JSONObject update(DemandEnterprise t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			service.update(t);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 修改状态视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "statusView")
	@RequiresPermissions("demandEnterprise:edit")
	public String statusView(String id, Model model) {
		DemandEnterprise t = service.getById(id);
		model.addAttribute("demandEnterprise", t);
		return "project/demandEnterprise/update_status";
	}

	/**
	 * 修改状态
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "updateStatus")
	@RequiresPermissions("demandEnterprise:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改企业招聘需求状态")
	public JSONObject updateStatus(String id, Integer status, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			DemandEnterprise demandEnterprise = service.getById(id);
			demandEnterprise.setStatus(status);
			service.update(demandEnterprise);
			json.put("code", 200);
			json.put("message", "状态修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "状态修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("demandEnterprise:delete")
	@SystemControllerLog(description = "删除企业招聘需求")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("demandEnterprise:detail")
	public String detailView(String id, Model model, CommonQuery query) {
		DemandEnterprise t = service.getById(id);
		if (t != null) {
			String companyId = t.getCompanyId();
			if (StringUtils.isNotEmpty(companyId)) {
				Company company = companyService.getById(companyId);
				if (company != null) {
					model.addAttribute("company", company);
				}
			}
			List<TalentsRecommend> recommends = recommendService.getByDemandId(id);
			if (!recommends.isEmpty() && recommends.size() > 0) {
				model.addAttribute("recommends", recommends);
				model.addAttribute("recommends_count", recommends.size());
			}
			List<DemandCommunicate> communicates = communicateService.getByDemandId(id);
			if(!communicates.isEmpty() && communicates.size()>0) {
				model.addAttribute("communicates", communicates);
				model.addAttribute("communicates_count", communicates.size());
			}
			Contact contact = contactService.getContactBy(companyId, t.getContact());
			if(contact!=null) {
				model.addAttribute("contact", contact);
			}
			model.addAttribute("demand", t);
			
		}
		return "project/demandEnterprise/detail";
	}

	/**
	 * @企业招聘需求管理列表视图
	 * @return
	 */
	@RequestMapping("/chooseDemand")
	public String chooseDemand(String position, String demandId,String companyId, Model model) {
		if (StringUtils.isEmpty(position)) {
			position = "";
		}
		if (StringUtils.isEmpty(demandId)) {
			demandId = "";
		}
		model.addAttribute("position", position);
		model.addAttribute("demandId", demandId);
		if (StringUtils.isEmpty(companyId)) {
			demandId = "";
		}
		model.addAttribute("companyId", companyId);
		return "project/demandEnterprise/choose_demand";
	}
}